package com.ggh.jinjilive.ui.mall.goods.affirm.adaoter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ItemShopPingCarAffirmBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.mobile.mall.goods.GoodsShopSPMobile;
import com.ggh.jinjilive.mobile.mall.shopping.ShopMessageMoblie;

import java.util.List;

public class ShoppingCarAffirmAdapter extends RecyclerView.Adapter<ShoppingCarAffirmAdapter.ItemViewHolder> {


    private List<ShopMessageMoblie> shopMessageMoblieList;

    public void setAdd(List<ShopMessageMoblie> shopMessageMoblieList) {
        this.shopMessageMoblieList = shopMessageMoblieList;
        notifyDataSetChanged();
    }


    public void Add(List<ShopMessageMoblie> shopMessageMoblieList) {
        if (null != this.shopMessageMoblieList) {
            this.shopMessageMoblieList.addAll(shopMessageMoblieList);
        } else {
            this.shopMessageMoblieList = shopMessageMoblieList;
        }
        notifyDataSetChanged();
    }


    /**
     * 更新数据
     */

    private AddItemOnClickListener addItemOnClickListener;

    public void setAddItemOnClickListener(AddItemOnClickListener addItemOnClickListener) {
        this.addItemOnClickListener = addItemOnClickListener;
    }


    @NonNull
    @Override
    public ShoppingCarAffirmAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemShopPingCarAffirmBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_shop_ping_car_affirm, parent, false);
        return new ShoppingCarAffirmAdapter.ItemViewHolder(binding);
    }


    public void onBindViewHolder(@NonNull ShoppingCarAffirmAdapter.ItemViewHolder holder, final int position) {
        ShopMessageMoblie shopMessageMoblie = shopMessageMoblieList.get(position);
        if (null != shopMessageMoblie) {
            //商品的适配器
            ShopingCarAffirmGoodsAdapter shopingCarAffirmGoodsAdapter = new ShopingCarAffirmGoodsAdapter();
            //店铺图片
            Glide.with(holder.binding.storeImg).load(shopMessageMoblie.getImg()).into(holder.binding.storeImg);
            //店铺名称
            holder.binding.storeName.setText(shopMessageMoblie.getName());
            //店铺商品
            if (null != shopMessageMoblie.getProduct() && shopMessageMoblie.getProduct().size() > 0) {
                shopingCarAffirmGoodsAdapter.setAddItemOnClickListener(new AddItemOnClickListener() {
                    @Override
                    public void onSkipClicked(int goodsPosition, Object mObject) {
                        List<GoodsShopSPMobile> goodsShopSPMobileList = (List<GoodsShopSPMobile>) mObject;
                        if (null != goodsShopSPMobileList && goodsShopSPMobileList.size() > 0) {
                            boolean allSelected = true;
                            for (GoodsShopSPMobile goodsShopSPMobile : goodsShopSPMobileList) {
                                if (!goodsShopSPMobile.isSelected()) {
                                    allSelected = false;
                                    break;
                                }
                            }
                            shopMessageMoblieList.get(position).setSelected(allSelected);
                        }
                        if (null != addItemOnClickListener) {
                            addItemOnClickListener.onSkipClicked(position, shopMessageMoblieList);
                        }
                    }
                });
                shopingCarAffirmGoodsAdapter.setAdd(shopMessageMoblieList.get(position).getProduct());
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(holder.binding.rvGoods.getContext(), LinearLayoutManager.VERTICAL, false) {
                    @Override
                    public boolean canScrollVertically() {
                        return false;
                    }
                };
                holder.binding.rvGoods.setLayoutManager(linearLayoutManager);
                holder.binding.rvGoods.setAdapter(shopingCarAffirmGoodsAdapter);

            }

        }
    }


    @Override
    public int getItemCount() {

        return null == shopMessageMoblieList ? 0 : shopMessageMoblieList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemShopPingCarAffirmBinding binding;

        public ItemViewHolder(@NonNull ItemShopPingCarAffirmBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}